package com.semidata.trp.business.utils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;

public class Utils {

    public static long getDate(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        return calendar.get(Calendar.YEAR) * 10000 + (calendar.get(Calendar.MONTH) + 1) * 100 + calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static byte[] readFile(File file) {

        ByteBuf buffer = Unpooled.buffer();

        FileInputStream in = null;

        try {
            in = new FileInputStream(file);

            while (in.available() > 0) {
                buffer.writeBytes(in, in.available());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }

        int size = buffer.writerIndex();
        byte[] bytes = new byte[size];
        buffer.getBytes(0, bytes);
        return bytes;
    }
}
